package cn.zbmao.web.util.dao;

import cn.zbmao.fc.FcTool;
import cn.zbmao.web.bean.Constants;
import cn.zbmao.web.services.QualificationServices;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

public class SqlUtil {

    /**
     *
     * @param map 用户设置的条件
     * @param tablename 表
     * @return
     * 重map中取出用户设置的条件，转换为sql语句返回
     */
	public static String getNewsWithUser(Map<String, List<Record>> map,String tablename){
		StringBuffer sb = new StringBuffer();
		List<Record> loc = map.get("user_district");
//        List<Record> locId=map.get("loc_id");
//取出用户设置的地域，拼接到SQL语句中
		if (loc.size()!=0) {
			for (int i = 0; i < loc.size(); i++) {
				if (i == 0) {
                    if(loc.get(i).getInt("loc_id")==Constants.LOC_ID_CITY){
                        sb.append(" ("+tablename+ Constants.CITY+"_id =" + loc.get(i).getInt("district_id"));
                    }else {
                        sb.append(" ("+tablename+ Constants.province+"_id =" + loc.get(i).getInt("district_id"));
                    }

				} else {
                    if(loc.get(i).getInt("loc_id")==Constants.LOC_ID_CITY){
                        sb.append(" or "+tablename+ Constants.CITY+"_id =" + loc.get(i).getInt("district_id"));
                    }else{
                        sb.append(" or "+tablename+ Constants.province+"_id =" + loc.get(i).getInt("district_id"));
                    }

				}
			}
			sb.append(") and");
		}
		
		List<Record> host = map.get("user_url");
		if (host.size()!=0) {

			for (int i = 0; i < host.size(); i++) {
				if (i == 0) {
					sb.append(" ("+tablename+"host_id =" + host.get(i).getInt("host_id"));
				} else {
					sb.append(" or "+tablename+"host_id =" + host.get(i).getInt("host_id"));
				}
			}
			sb.append(") and");
		}
//		取出用户关注的关键字与资质，拼接到SQL语句中
		List<Record> keyword = map.get("user_keyword");
        List<Record> qualification = map.get("user_qualification");
        if (keyword.size()!=0 || qualification.size()!=0) {
			sb.append(" match("+tablename+"content_fc) against ('");
			for (int i = 0; i < keyword.size(); i++) {
				if (i == 0) {
					sb.append(getFCkeyword(keyword.get(i).getStr("keyword")));
				} else {
					sb.append(" " + getFCkeyword(keyword.get(i).getStr("keyword")));
				}
			}
            for (int i = 0; i < qualification.size(); i++) {
                if (qualification.get(i).getInt("is_ok")==0){
                    continue;
                }
                String[] str = QualificationServices.getQualificationMap().get(qualification.get(i).getInt("qualification_id")).getSeachTerm();
                for (String string : str) {
                    if (StringUtils.isBlank(string)) {
                        break;
                    }
                    sb.append(" "+string);
                }
            }
			sb.append("' in boolean mode) and");
		}
		return sb.toString();
	}

    /**
     *
     * @param map 用户添加的设置
     * @param tablename 表
     * @return
     * 根据用户设置的属性经行数据查询
     */
    public static String getNewsWithUserAnd(Map<String, List<Record>> map,String tablename){
        StringBuffer sb = new StringBuffer();
        List<Record> loc = map.get("user_district");
//地域选择判断
        if (loc.size()!=0) {
            for (int i = 0; i < loc.size(); i++) {
                if (i == 0) {
                    if(loc.get(i).getInt("loc_id")==Constants.LOC_ID_CITY){
                        sb.append(" ("+tablename+ Constants.CITY+"_id =" + loc.get(i).getInt("district_id"));
                    }else {
                        sb.append(" ("+tablename+ Constants.province+"_id =" + loc.get(i).getInt("district_id"));
                    }

                } else {
                    if(loc.get(i).getInt("loc_id")==Constants.LOC_ID_CITY){
                        sb.append(" or "+tablename+ Constants.CITY+"_id =" + loc.get(i).getInt("district_id"));
                    }else{
                        sb.append(" or "+tablename+ Constants.province+"_id =" + loc.get(i).getInt("district_id"));
                    }

                }
            }
            sb.append(") and");
        }

        List<Record> host = map.get("user_url");
        if (host.size()!=0) {

            for (int i = 0; i < host.size(); i++) {
                if (i == 0) {
                    sb.append(" ("+tablename+"host_id =" + host.get(i).getInt("host_id"));
                } else {
                    sb.append(" or "+tablename+"host_id =" + host.get(i).getInt("host_id"));
                }
            }
            sb.append(") and");
        }
//通过用户设置的关键字与资质拼接到查询的sql语句中
        List<Record> keyword = map.get("user_keyword");
        List<Record> qualification = map.get("user_qualification");
        if (keyword.size()!=0 || qualification.size()!=0) {
            sb.append(" match("+tablename+"content_fc) against ('");
            for (int i = 0; i < keyword.size(); i++) {
                if (i == 0) {
                    sb.append(getFCkeyword1(keyword.get(i).getStr("keyword"),keyword.size()));
                } else {
                    sb.append(" " + getFCkeyword2(keyword.get(i).getStr("keyword"),i, keyword.size()));
                }
            }
            for (int i = 0; i < qualification.size(); i++) {
                if (qualification.get(i).getInt("is_ok")==0){
                    continue;
                }
                String[] str = QualificationServices.getQualificationMap().get(qualification.get(i).getInt("qualification_id")).getSeachTerm();
//                for (String string : str) {
//                    if (StringUtils.isBlank(string)) {
//                        break;
//                    }
//                    sb.append(" "+string);
//                }
                for(int a=0;a<str.length;a++){
                    if(a==0){

                        if(str.length==1){
                            sb.append(" +("+str[a]+")");
                        }else{
                            sb.append(" +("+str[a]);
                        }
                    }else if(a==str.length-1){
                        sb.append(" "+str[a]+")");
                    }else{
                        sb.append(" "+str[a]);
                    }
                }
            }
            sb.append("' in boolean mode) and");
        }
        return sb.toString();
    }

    //(+福田区 +人民法院)=福田区人民法院

    /**
     *
     * @param keyword
     * @return
     * 对关键字经行分词处理，并以字符串形式返回
     */
    public  static String getFCkeyword(String keyword){


        keyword = FcTool.getFc().indexFc(keyword);
        StringBuffer stringBuffer = new StringBuffer("(");
        String[] keys = keyword.split(" ");
        for (String key:keys){
            stringBuffer.append("+"+key+" ");
        }
        stringBuffer.deleteCharAt(stringBuffer.length()-1);
        stringBuffer.append(")");

        return stringBuffer.toString();
    }

    private static String getFCkeyword1(String keyword,int size){

        keyword = FcTool.getFc().indexFc(keyword);
        StringBuffer stringBuffer=null;
        if(size==1){
            stringBuffer = new StringBuffer("+(");
        }else{
            stringBuffer = new StringBuffer("+((");
        }

        String[] keys = keyword.split(" ");
        for (String key:keys){
            stringBuffer.append("+"+key+" ");
        }
        stringBuffer.deleteCharAt(stringBuffer.length()-1);
        stringBuffer.append(")");

        return stringBuffer.toString();
    }
    private static String getFCkeyword2(String keyword,int i,int size){
        StringBuffer stringBuffer=null;
        if(i==size-1){
            keyword = FcTool.getFc().indexFc(keyword);
            stringBuffer = new StringBuffer("(");
            String[] keys = keyword.split(" ");
            for (String key:keys){
                stringBuffer.append("+"+key+" ");
            }
            stringBuffer.deleteCharAt(stringBuffer.length()-1);
            stringBuffer.append("))");
        }else{
            keyword = FcTool.getFc().indexFc(keyword);
            stringBuffer = new StringBuffer("(");
            String[] keys = keyword.split(" ");
            for (String key:keys){
                stringBuffer.append("+"+key+" ");
            }
            stringBuffer.deleteCharAt(stringBuffer.length()-1);
            stringBuffer.append(")");
        }


        return stringBuffer.toString();
    }


    public static void main(String[] args) {
//        String s = FcUtil.fc("福田区人民法院");
        System.out.println(getFCkeyword("福田区人民法院"));
    }
}
